﻿#pragma once
#include "LuaCommon.h"
#include <stdio.h>
#include <string.h>

#if _MSC_VER
#define snprintf _snprintf
#endif


class LUAOBJ_API LuaException
{
public:
	LuaException(const std::string & w):m_what(w){}
	LuaException(const char* w):m_what(w){}

	LuaException(const char* w,const char* file,int line){
		char buf[512];
		int len = snprintf(buf,sizeof(buf),"LuaException:%s file:%s line:%d",w, FileNameFromPath(file),line);
		m_what.assign(buf,len);
	}

	inline const char* what() {
		return m_what.c_str();
	}
private:
	const char* FileNameFromPath(const char* file) {
		const char* end1 = ::strrchr(file, '/');
		const char* end2 = ::strrchr(file, '\\');
		if (!end1 && !end2)
			return file;
		else
			return (end1 > end2) ? end1 + 1 : end2 + 1;
	}

	std::string m_what;
};

typedef int (*LuaErrorHandler) (const char* err);


#define LUA_THROW_EXCEPTION(w) throw LuaException(w,__FILE__,__LINE__)
